package cn.hncu.booksManagementSystem.user.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import cn.hncu.booksManagementSystem.BMS_APP;
import cn.hncu.booksManagementSystem.user.business.ebi.UserEbi;
import cn.hncu.booksManagementSystem.user.business.factory.UserEbiFactory;
import cn.hncu.booksManagementSystem.user.vo.UserQueryModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class LoginPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tfdName; //用户名输入框
	private JPasswordField tfdPwd; //密码输入框
	private JFrame mainFrame; //主框架
	//注入ebi
	private UserEbi ebi = UserEbiFactory.getInstanceOfUserEbi();
	private JButton btnLogin;
	
	/**
	 * Create the panel.
	 */
	public LoginPanel(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		setMinimumSize(new Dimension(800, 600));
		setLayout(null);
		
		JLabel label = new JLabel("用户登入");
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(320, 47, 157, 51);
		add(label);
		
		JLabel label_1 = new JLabel("用户名：");
		label_1.setForeground(Color.BLUE);
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_1.setBounds(232, 186, 100, 36);
		add(label_1);
		
		tfdName = new JTextField();
		tfdName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode()==KeyEvent.VK_ENTER) {
					tfdPwd.requestFocus();
				}
			}
		});
		tfdName.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdName.setBounds(335, 186, 189, 36);
		add(tfdName);
		tfdName.setColumns(10);
		
		JLabel label_2 = new JLabel("密   码：");
		label_2.setForeground(Color.BLUE);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_2.setBounds(232, 267, 100, 36);
		add(label_2);
		
		tfdPwd = new JPasswordField();
		tfdPwd.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode()==KeyEvent.VK_ENTER) {
					btnLogin.requestFocus();
				}
			}
		});
		tfdPwd.setBounds(335, 267, 189, 36);
		add(tfdPwd);
		
		btnLogin = new JButton("登入");
		btnLogin.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode()==KeyEvent.VK_ENTER) {
					btnLoginActionPerformed(null);
				}
			}
		});
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnLoginActionPerformed(e);
			}
		});
		btnLogin.setForeground(new Color(0, 0, 255));
		btnLogin.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnLogin.setBounds(252, 373, 93, 36);
		add(btnLogin);
		
		JButton btnReset = new JButton("重置");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tfdName.setText("");
				tfdPwd.setText("");
			}
		});
		btnReset.setForeground(Color.BLUE);
		btnReset.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnReset.setBounds(417, 373, 93, 36);
		add(btnReset);

	}

	private void btnLoginActionPerformed(ActionEvent e) {
		//1 收集参数(name,pwd) 缺少 id,type
		String name = tfdName.getText();
		if (name==null || name.trim().length()==0) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}
		boolean isLegal = name.matches("^\\w{3,10}$");
		if (!isLegal) {
			JOptionPane.showMessageDialog(null, "用户名格式错误，只能由字母、数字和_组成，并且长度在3-10之间");
			return;
		}
		@SuppressWarnings("deprecation")
		String pwd = tfdPwd.getText();
		if (pwd==null|| pwd.trim().length()==0) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}
		isLegal = pwd.matches("^\\w{3,10}$");
		if (!isLegal) {
			JOptionPane.showMessageDialog(null, "密码格式错误，只能由字母、数字和_组成，并且长度在3-10之间");
			return;
		}
		
		//2 组织参数
		UserQueryModel uqm = new UserQueryModel();
		uqm.setName(name);
		uqm.setPwd(pwd);
		
		//3 调用逻辑层
		BMS_APP.activeUser = ebi.login(uqm);
		
		//4导向结果界面
		if (BMS_APP.activeUser!=null) {
//			JOptionPane.showMessageDialog(null, "登入成功！！");
			mainFrame.setContentPane(new WelcomePanel(mainFrame));
			mainFrame.validate();
		}else {
			JOptionPane.showMessageDialog(null, "用户名或密码错误,请从新输入！");
			//用户名保留
			//密码清空
			tfdPwd.setText("");
			return;
		}
	}
}
